package com.hitqz.robot.biz.flow.component.agv;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.api.common.entity.Robot;
import com.hitqz.robot.api.common.entity.RobotMapPos;
import com.hitqz.robot.api.common.entity.TicketsTask;
import com.hitqz.robot.biz.service.RobotMapPosService;
import com.hitqz.robot.biz.service.RobotService;
import com.hitqz.robot.driver.model.IRobotPosDto;
import com.hitqz.robot.driver.parent.RobotDriverService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

/**
 * @author xupkun
 * @date 2024/11/5
 */
@Slf4j
@LiteflowComponent(value = "NotifyServerArrivePos", name = "[轮对/轮轴]到达当前点位通知")
@AllArgsConstructor
public class NotifyServerArriveSourcePos extends NodeComponent {

    private final RestTemplate restTemplate;

    private final RobotService robotService;

    private final RobotMapPosService robotMapPosService;

    private final RobotDriverService robotDriverService;
    // 91102---911012
    @Override
    public void process() throws Exception {
        Robot robot = robotService.getDefaultRobot();
        FlowContextDto dto =  this.getRequestData();
        TicketsTask task = dto.getTicketsTask();
        FlowPatrolPreAndEndTicketsDto ticketsDto = JSONObject.parseObject(dto.getTicketsTask()
                .getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
        IRobotPosDto iRobotPosDto = robotDriverService.getWebPos();
        RobotMapPos robotMapPos = robotMapPosService.getOne(Wrappers.<RobotMapPos>lambdaQuery()
                .eq(RobotMapPos::getCode, iRobotPosDto.getCurrentStation())
                .eq(RobotMapPos::getMapCode, robot.getMapCode()));
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.valueOf("application/json;UTF-8"));
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("sbID", robot.getRobotSn());
            jsonObject.put("fktime", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            jsonObject.put("arrivePos", robotMapPos.getName());
            HttpEntity<String> strEntity = new HttpEntity<String>(jsonObject.toString(), headers);
            log.info("请求参数:" + strEntity.toString());
            String result = restTemplate.postForObject("http://172.24.3.1:8090/api/v1/centreApp/agv/arrive/",
                    strEntity, String.class);
            log.info("reset session" + result);
        }catch (Exception e) {
            log.info("上报到达位置");
        }

    }
}
